<%
 /**
  * workoutsCalendar.jsp
  * 
  * Displays a calendar for the user to pick a date of their workout.
  * 
  * The first page of a sequence of forms requisitioning the user for the information about their workout.
  * The complete sequence is:
  * workoutsCalendar.jsp -- Gets date
  * workouts.jsp -- Gets information common between workout types: userID, prescribed, and type
  * workoutsCreateWorkout.jsp -- redirect dummy page, creates the actual workout object and puts in the fields 
  * 	we've already gotten from the user
  * workoutsAMRAP.jsp/workoutsTimed.jsp/workoutsTabata.jsp -- Gets the type-specific workout information
  * workoutsCreateWorkoutTyped.jsp -- redirect dummy page, adds the typed information to our Workout object
  * workoutsAddExercise.jsp -- Allows the user to select individual exercises and append them to the list in our
  *		exercise.
  * workoutsCreateExercise.jsp -- redirect dummy page, adds the exercise to our Workout object
  * workoutsSubmit.jsp -- final page, submits the fully created workout to the database
  *
  * @author Chris
  */

%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%@page import="b1w1nn1ng.crossf1t.db.*"%>
<%@page import="b1w1nn1ng.crossf1t.gui.*"%>
<%@page import="b1w1nn1ng.crossf1t.business.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Workout</title>
<link rel="stylesheet" type="text/css" href="/crossfit/css/general.css" />
<link rel="stylesheet" type="text/css" href="/crossfit/css/calendar.css" />
</head>
<%
	User user = (User) session.getAttribute("user");
	session.setAttribute("user", user);
%>
<%!Calendar cal;
	Calendar today; //always holds the current date
	String month[] = { "January", "February", "March", "April", "May", "June",
			"July", "August", "September", "October", "November", "December" };
	String day[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };%>

<body>
	<div id="container">
		<jsp:include page="/navbar.jsp" />
		<h1>Add Workout</h1>
		<hr />

		Please select a date for your workout:
		
		<div id="calendar">
			<table id="cal">
				<%
					//this part sets up the calendar
					cal = CalendarDisplay.getCalendar(request); //gets the current month
					today = CalendarDisplay.currCal(); //sets today to current date
					String title = month[cal.get(Calendar.MONTH)] + " "
							+ cal.get(Calendar.YEAR);
					String daysOfWeek = "<tr>";
					for (int i = 0; i < 7; i++)
						daysOfWeek += "<td>" + day[i] + "</td>"; //prints out the days of the week mon, tue....
					daysOfWeek += "</tr>";
					cal.set(Calendar.DATE, 1);
					int day = cal.get(Calendar.DAY_OF_WEEK);
					int currMonth = cal.get(Calendar.MONTH);
					int i = 1; //i will be the value to print;
					String offset = "";
					for (int j = 1; j < day; j++)
						offset += "<td></td>";
					String back = "<<";
					String forward = ">>";
				%>
				<!-- calendar setup complete -->
				<tr><td><a href="workoutsCalendarBack.jsp"><%=back%></a></td><td colspan=5 align="center"><%=title%></td><td><a href="workoutsCalendarForward.jsp"><%=forward%></a></td></tr>
				<%=daysOfWeek%>
				<%
					//this part prints the calendar
					int date;
					out.print("<tr>" + offset);
					while (cal.get(Calendar.MONTH) == currMonth) {
						date = cal.get(Calendar.DATE);
						out.print("<td ");
						if(CalendarDisplay.equals(cal, today))
						out.print(" class=\"currDay\" ");
						out.print("id=\"calNoWorkout\">"
								+ "<a class=\"cal\" href=\"workouts.jsp?date=" + CalendarDisplay.dateToString(cal) + "\" >" + date
								+ "</a></td>");
						session.setAttribute("date", CalendarDisplay.dateToString(cal));
						
						cal.add(Calendar.DATE, 1);
						if ((date + day - 1) % 7 == 0)
							out.print("</tr><tr>");
					}
					out.print("</tr>");
					cal.add(Calendar.DATE, -1); //resets calendar back one day because after
					//the loop finishes running the calendar was on the next month causing a bug with the back button displaying the next month.
				%>
			</table>
		</div>
		<input type="button" value="Cancel" onclick="window.location='home.jsp'" />
	</div>
</body>
</html>